/**
 * Application Name: TCSS422_WebCrawler
 * Group Name: The Other Guys
 * Members: Scott Freeman, Anthony Melcher, Jason Green
 * Date: November 10, 2011
 * 
 * Related libraries: Jericho HTML Page Parser (author unknown) 
 * 					  http://jericho.htmlparser.net/docs/index.html
 */
package report;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

import controller.Spider;

/**
 * This class reports various statistics to the different
 * appropriate output files.
 */
public final class Reporter {
	/**
	 * The stream to write data to a file.
	 */
	private static BufferedWriter out = null;
	
	/**
	 * Writes all relevant statistical data to a file.
	 * All data is cumulative based on the number of pages parsed
	 * by the time it is to display.
	 */
	private static void writeToFile(final String formatted_data) {
		
		try {
			out = new BufferedWriter(new FileWriter("reporterOutput.txt",true));
			out.write(formatted_data);
			out.close();
		} catch(final IOException e) {
			// do nothing
		}
	}
	
	/**
	 * Writes all relevant statistical data to the console.
	 * All data is cumulative based on the number of pages parsed
	 * by the time it is to display.
	 */
	public static void displayOnConsole(
			final String page_name,
			final int page_count,
			final int total_words,
			final double words_per_page,
			final double url_per_page,
			final double avg_parse_time,
			final double total_duration){
		
		StringBuilder formatted_data = new StringBuilder();
		
		formatted_data.append("\n ___________________________________________________________\n\n");
		//First section of the output.
		formatted_data.append(String.format("  Parsed: %s\n\n  Pages Retrieved: %s\n",  page_name, page_count));
		formatted_data.append(String.format("  Average words per page: %s\n  Average URLs per page: %s\n", words_per_page, url_per_page));
		formatted_data.append(String.format("\n  %-18s%-27s%s\n", "KEYWORD", "AVG HITS PER PAGE", "TOTAL HITS"));
		formatted_data.append(" ----------------------------------------------------------\n");

		float total = 0;
		for(String keyword : Spider.keywordsMap.keySet()) {
			total = Spider.keywordsMap.get(keyword).get();
			
			formatted_data.append(String.format("  %-20s\t%4.5f%24s\n", keyword, (total / total_words), total));
		}
		
		formatted_data.append(String.format("\n  Page Limit: %s\n", Spider.maxPages.get()));
		formatted_data.append(String.format("  Average parse time per page: %5.1f msec\n", avg_parse_time));
		formatted_data.append(String.format("  Total Running Time: %5.3f sec\n", total_duration));
		
		System.out.println(formatted_data.toString());
		writeToFile(formatted_data.toString());	
	}

	/**
	 * Records 
	 * @param avg_parse_time
	 * @param total_duration
	 */
	public static void recordDuration(final double avg_parse_time, final double total_duration) {
		
		try {
			if(Spider.file_out_selection) { // Single Threaded Output
				out = new BufferedWriter(new FileWriter("spiderRunST.txt",true));
				out.write("Avg Parse Time : " + avg_parse_time + "; Total Duration: " + total_duration + "\n");
				out.close();
			} else { // Multi Threaded Output
				out = new BufferedWriter(new FileWriter("spiderRunMT.txt",true));
				out.write("Avg Parse Time : " + avg_parse_time + "; Total Duration: " + total_duration + "\n");
				out.close();
			}
		} catch(final IOException e) {
			// do nothing for now
		}		
	}
}
